﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Xajhzwb
{
    public partial class FormUrlThunder : Form
    {
        private string[] strs1 = new string[] { "迅雷", "快车", "旋风", "RayFile" };
        private string[] strs2 = new string[] { "普通" };
 
        public FormUrlThunder()
        {
            InitializeComponent();
        }

        private void FormUrlThunder_Load(object sender, EventArgs e)
        {
            ChangeCboItems(cboFormat, cboNormal);
        }

        private void FormUrlThunder_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
        }

        private void lklblChange_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.cboNormal.Items.Count == 1)
            {
                this.ChangeCboItems(this.cboNormal, this.cboFormat);
            }
            else
            {
                this.ChangeCboItems(this.cboFormat, this.cboNormal);
            }
            this.cboFormat.SelectedIndex = 0;
            this.cboNormal.SelectedIndex = 0;
        }

        public void ChangeCboItems(ComboBox cbo1, ComboBox cbo2)
        {
            cbo1.Items.Clear();
            cbo2.Items.Clear();
            for (int i = 0; i < this.strs1.Length; i++)
            {
                cbo1.Items.Add(this.strs1[i]);
            }
            cbo2.Items.Add(this.strs2[0]);
            cbo1.SelectedIndex = 0;
            cbo2.SelectedIndex = 0;
        }

        private void btnTransfer_Click(object sender, EventArgs e)
        {
            string str = "";
            if (this.rtbLink.Text.Trim() == "")
            {
                this.rtbResult.Text = "请输入链接地址";
            }
            else
            {
                string[] lines = rtbLink.Lines;
                string[] results = new string[lines.Length];

                for (int i =0;i<lines.Length;i++)
                {
                    if (this.cboFormat.Items.Count == 4)
                    {
                        str = this.Format2Normal(lines[i], this.cboFormat.SelectedItem.ToString());
                    }
                    else
                    {
                        str = this.Normal2Format(lines[i], this.cboNormal.SelectedItem.ToString());
                    }

                    results[i] = str;
                }

                rtbResult.Lines = results;
            }
        }


        public string Format2Normal(string formatLink, string type)
        {
            int num;
            string str = "专用链接地址不正确";
            if ((type == "迅雷") && formatLink.ToLower().Contains("thunder://"))
            {
                num = 0;
                num = formatLink.LastIndexOf("/") + 1;
                formatLink = formatLink.Substring(num, formatLink.Length - num);
                str = this.Base64ToStr(formatLink);
                if (str == "专用链接地址不正确")
                {
                    return str;
                }
                return str.Substring(2, str.Length - 4);
            }
            if ((type == "快车") && formatLink.ToLower().Contains("flashget://"))
            {
                int index = 0;
                num = formatLink.LastIndexOf("/") + 1;
                index = formatLink.IndexOf("&");
                if (index == -1)
                {
                    index = formatLink.Length;
                }
                formatLink = formatLink.Substring(num, index - num);
                str = this.Base64ToStr(formatLink);
                if (str == "专用链接地址不正确")
                {
                    return str;
                }
                return str.Replace("[FLASHGET]", "");
            }
            if ((type == "旋风") && formatLink.ToLower().Contains("qqdl://"))
            {
                num = 0;
                num = formatLink.LastIndexOf("/") + 1;
                formatLink = formatLink.Substring(num, formatLink.Length - num);
                str = this.Base64ToStr(formatLink);
                if (str == "专用链接地址不正确")
                {
                    return str;
                }
                return str;
            }
            if ((type == "RayFile") && formatLink.ToLower().Contains("fs2you://"))
            {
                int length = 0;
                num = formatLink.LastIndexOf("/") + 1;
                formatLink = formatLink.Substring(num, formatLink.Length - num);
                str = this.Base64ToStr(formatLink);
                if (str == "专用链接地址不正确")
                {
                    return str;
                }
                length = str.IndexOf("|");
                if (length == -1)
                {
                    length = str.Length;
                }
                str = "http://" + str.Substring(0, length);
            }
            return str;
        }

        public string Normal2Format(string normalLink, string type)
        {
            string s = "";
            if (!normalLink.Contains("http://"))
            {
                normalLink = "http://" + normalLink;
            }
            if (type == "迅雷")
            {
                s = "AA" + normalLink + "ZZ";
                s = Convert.ToBase64String(Encoding.Default.GetBytes(s));
                return ("thunder://" + s);
            }
            if (type == "快车")
            {
                s = "[FLASHGET]" + normalLink + "[FLASHGET]";
                s = Convert.ToBase64String(Encoding.Default.GetBytes(s));
                return ("Flashget://" + s);
            }
            if (type == "旋风")
            {
                s = Convert.ToBase64String(Encoding.Default.GetBytes(normalLink));
                return ("qqdl://" + s);
            }
            if (type == "RayFile")
            {
                normalLink = normalLink.Replace("http://", "");
                s = Convert.ToBase64String(Encoding.Default.GetBytes(normalLink));
                s = "fs2you://" + s;
            }
            return s;
        }

        public string Base64ToStr(string str)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String(str);
                return Encoding.Default.GetString(bytes);
            }
            catch (Exception)
            {
                return "专用链接地址不正确";
            }
        }

 


    }
}